package com.xxc.domain.strategy.service.raffle;

import com.xxc.domain.strategy.model.entity.StrategyAwardEntity;
import com.xxc.domain.strategy.model.valobj.RuleTreeVO;
import com.xxc.domain.strategy.model.valobj.StrategyAwardRuleModelVO;
import com.xxc.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import com.xxc.domain.strategy.repository.IStrategyRepository;
import com.xxc.domain.strategy.service.AbstractRaffleStrategy;
import com.xxc.domain.strategy.service.IRaffleAward;
import com.xxc.domain.strategy.service.IRaffleStock;
import com.xxc.domain.strategy.service.armory.IStrategyDispatch;
import com.xxc.domain.strategy.service.rule.chain.ILogicChain;
import com.xxc.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import com.xxc.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import com.xxc.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xxc
 * @Description: 默认的抽奖策略实现
 * @Date: 2025/1/25 14:23
 * @Version: 1.0
 */

@Slf4j
@Service
public class DefaultRaffleStrategy extends AbstractRaffleStrategy implements IRaffleStock, IRaffleAward {

    public DefaultRaffleStrategy(DefaultChainFactory defaultChainFactory, DefaultTreeFactory defaultTreeFactory, IStrategyDispatch strategyDispatch, IStrategyRepository repository) {
        super(defaultChainFactory, defaultTreeFactory, strategyDispatch, repository);
    }

    @Override
    protected DefaultChainFactory.StrategyAwardVO raffleLogicChain(String userId, Long strategyId) {
        // 1. 获取抽奖前置规则责任链 - 前置规则的责任链处理
        ILogicChain logicChain = defaultChainFactory.openLogicChain(strategyId);
        // 2. 通过责任链获得处理后的StrategyAwardVO
        return logicChain.logic(userId, strategyId);
    }

    @Override
    public DefaultTreeFactory.StrategyAwardVO raffleLogicTree(String userId, Long strategyId, Integer awardId) {
        // 1. 查询该策略id+奖品id配置的规则
        StrategyAwardRuleModelVO strategyAwardRuleModelVO = repository.queryStrategyAwardRuleModelVO(strategyId, awardId);
        // 若该策略id+奖品id下未配置规则，返回
        if (null == strategyAwardRuleModelVO) {
            return DefaultTreeFactory.StrategyAwardVO.builder()
                    .awardId(awardId)
                    .build();
        }

        // 2. 获取规则树的配置
        RuleTreeVO ruleTreeVO = repository.queryRuleTreeVOByTreeId(strategyAwardRuleModelVO.getRuleModels());
        if (null == ruleTreeVO){
            throw new RuntimeException("存在抽奖策略配置的规则模型 Key，未在库表 rule_tree、rule_tree_node、rule_tree_line 配置对应的规则树信息 " + strategyAwardRuleModelVO.getRuleModels());
        }

        // 3. 获取抽奖中/后规则树，进行处理并返回
        IDecisionTreeEngine treeEngine = defaultTreeFactory.openLogicTree(ruleTreeVO);
        return treeEngine.process(userId, strategyId, awardId);
    }

    @Override
    public StrategyAwardStockKeyVO takeQueueValue() throws InterruptedException {
        return repository.takeQueueValue();
    }

    @Override
    public void updateStrategyAwardStock(Long strategyId, Integer awardId) {
        repository.updateStrategyAwardStock(strategyId, awardId);
    }

    @Override
    public List<StrategyAwardEntity> queryRaffleStrategyAwardList(Long strategyId) {
        return repository.queryStrategyAwardList(strategyId);
    }
}